<template><div><p>本文档最新版为 <a href="https://learnku.com/docs/laravel/10.x" target="_blank" rel="noopener noreferrer">10.x</a>，旧版本可能放弃维护，推荐阅读最新版！</p>
<h2 id="控制台测试" tabindex="-1"><a class="header-anchor" href="#控制台测试"><span>控制台测试</span></a></h2>
<ul>
<li><a href="#introduction">介绍</a></li>
<li><a href="#success-failure-expectations">期望成功 / 失败</a></li>
<li><a href="#input-output-expectations">期望输入 / 输出</a></li>
<li><a href="#console-events">控制台事件</a></li>
</ul>
<h2 id="介绍" tabindex="-1"><a class="header-anchor" href="#介绍"><span>介绍</span></a></h2>
<p>除了简化 HTTP 测试之外，Laravel 还提供了一个简单的 API 来测试应用程序的 <a href="https://learnku.com/docs/laravel/11.x/artisan" target="_blank" rel="noopener noreferrer">自定义控制台命令</a>。</p>
<h2 id="期望成功-失败" tabindex="-1"><a class="header-anchor" href="#期望成功-失败"><span>期望成功/失败</span></a></h2>
<p>首先，让我们探索如何对 Artisan 命令的退出代码进行断言。为此，我们将使用 <code v-pre>artisan</code> 方法从我们的测试中调用 Artisan 命令。然后，我们将使用 <code v-pre>assertExitCode</code> 方法断言该命令以给定的退出代码完成:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token function">test</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'console command'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">artisan</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'inspire'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">assertExitCode</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 测试控制台命令。</span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">test_console_command</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword return-type">void</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">artisan</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'inspire'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">assertExitCode</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>你可以使用 <code v-pre>assertNotExitCode</code> 方法断言命令没有以给定的退出代码退出：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$this</span><span class="token operator">-></span><span class="token function">artisan</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'inspire'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">assertNotExitCode</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>当然，所有终端命令通常在成功时以 <code v-pre>0</code> 状态码退出，在不成功时以非零退出码退出。因此，为方便起见，你可以使用 <code v-pre>assertSuccessful</code> 和 <code v-pre>assertFailed</code> 断言来断言给定命令是否以成功的退出代码退出：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$this</span><span class="token operator">-></span><span class="token function">artisan</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'inspire'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">assertSuccessful</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$this</span><span class="token operator">-></span><span class="token function">artisan</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'inspire'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">assertFailed</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="期望输入-输出" tabindex="-1"><a class="header-anchor" href="#期望输入-输出"><span>期望输入/输出</span></a></h2>
<p>Laravel 允许你使用 <code v-pre>expectsQuestion</code> 方法轻松 「mock」控制台命令的用户输入。此外，你可以使用 <code v-pre>assertExitCode</code> 和 <code v-pre>expectsOutput</code> 方法指定你希望通过控制台命令输出的退出代码和文本。例如，考虑以下控制台命令:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Artisan</span><span class="token operator">::</span><span class="token function">command</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'question'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token variable">$name</span> <span class="token operator">=</span> <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">ask</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'What is your name?'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$language</span> <span class="token operator">=</span> <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">choice</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Which language do you prefer?'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'PHP'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token string single-quoted-string">'Ruby'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token string single-quoted-string">'Python'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">line</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Your name is '</span><span class="token operator">.</span><span class="token variable">$name</span><span class="token operator">.</span><span class="token string single-quoted-string">' and you prefer '</span><span class="token operator">.</span><span class="token variable">$language</span><span class="token operator">.</span><span class="token string single-quoted-string">'.'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>你可以通过以下测试来测试该命令:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token function">test</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'console command'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">artisan</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'question'</span><span class="token punctuation">)</span></span>
<span class="line">         <span class="token operator">-></span><span class="token function">expectsQuestion</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'What is your name?'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'Taylor Otwell'</span><span class="token punctuation">)</span></span>
<span class="line">         <span class="token operator">-></span><span class="token function">expectsQuestion</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Which language do you prefer?'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'PHP'</span><span class="token punctuation">)</span></span>
<span class="line">         <span class="token operator">-></span><span class="token function">expectsOutput</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Your name is Taylor Otwell and you prefer PHP.'</span><span class="token punctuation">)</span></span>
<span class="line">         <span class="token operator">-></span><span class="token function">doesntExpectOutput</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Your name is Taylor Otwell and you prefer Ruby.'</span><span class="token punctuation">)</span></span>
<span class="line">         <span class="token operator">-></span><span class="token function">assertExitCode</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * Test a console command.</span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">test_console_command</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword return-type">void</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">artisan</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'question'</span><span class="token punctuation">)</span></span>
<span class="line">         <span class="token operator">-></span><span class="token function">expectsQuestion</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'What is your name?'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'Taylor Otwell'</span><span class="token punctuation">)</span></span>
<span class="line">         <span class="token operator">-></span><span class="token function">expectsQuestion</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Which language do you prefer?'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'PHP'</span><span class="token punctuation">)</span></span>
<span class="line">         <span class="token operator">-></span><span class="token function">expectsOutput</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Your name is Taylor Otwell and you prefer PHP.'</span><span class="token punctuation">)</span></span>
<span class="line">         <span class="token operator">-></span><span class="token function">doesntExpectOutput</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Your name is Taylor Otwell and you prefer Ruby.'</span><span class="token punctuation">)</span></span>
<span class="line">         <span class="token operator">-></span><span class="token function">assertExitCode</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>你还可以使用<code v-pre>doesntExpectOutput</code>方法断言控制台命令不会生成任何输出:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token function">test</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'console command'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">artisan</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'example'</span><span class="token punctuation">)</span></span>
<span class="line">         <span class="token operator">-></span><span class="token function">doesntExpectOutput</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">         <span class="token operator">-></span><span class="token function">assertExitCode</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * Test a console command.</span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">test_console_command</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword return-type">void</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">artisan</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'example'</span><span class="token punctuation">)</span></span>
<span class="line">            <span class="token operator">-></span><span class="token function">doesntExpectOutput</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">            <span class="token operator">-></span><span class="token function">assertExitCode</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p><code v-pre>expectsOutputToContain</code> 和 <code v-pre>doesntExpectOutputToContain</code> 方法可用于对输出的一部分进行断言</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token function">test</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'console command'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">artisan</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'example'</span><span class="token punctuation">)</span></span>
<span class="line">         <span class="token operator">-></span><span class="token function">expectsOutputToContain</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Taylor'</span><span class="token punctuation">)</span></span>
<span class="line">         <span class="token operator">-></span><span class="token function">assertExitCode</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * Test a console command.</span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">test_console_command</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword return-type">void</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">artisan</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'example'</span><span class="token punctuation">)</span></span>
<span class="line">            <span class="token operator">-></span><span class="token function">expectsOutputToContain</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Taylor'</span><span class="token punctuation">)</span></span>
<span class="line">            <span class="token operator">-></span><span class="token function">assertExitCode</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="确认期望" tabindex="-1"><a class="header-anchor" href="#确认期望"><span>确认期望</span></a></h4>
<p>当编写一个期望以「是」或「否」答案形式确认的命令时，你可以使用 <code v-pre>expectsConfirmation</code> 方法</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$this</span><span class="token operator">-></span><span class="token function">artisan</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'module:import'</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">expectsConfirmation</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Do you really wish to run this command?'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'no'</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">assertExitCode</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="表期望值" tabindex="-1"><a class="header-anchor" href="#表期望值"><span>表期望值</span></a></h4>
<p>如果命令使用 Artisan 的 <code v-pre>table</code> 方法显示信息表，则为整个表编写输出期望值可能会很麻烦。相反，您可以使用<code v-pre>expectsTable</code>方法。此方法接受表的标头作为其第一个参数，并将表的数据作为其第二个参数：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$this</span><span class="token operator">-></span><span class="token function">artisan</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'users:all'</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">expectsTable</span><span class="token punctuation">(</span><span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'ID'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token string single-quoted-string">'Email'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'taylor@example.com'</span><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'abigail@example.com'</span><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="控制台事件" tabindex="-1"><a class="header-anchor" href="#控制台事件"><span>控制台事件</span></a></h2>
<p>默认情况下，在运行应用程序的测试时，不会调度<code v-pre>Illuminate\Console\Events\CommandStarting</code>和<code v-pre>Illuminate\Console\Events\CommandFinished</code>事件。但是，您可以通过将<code v-pre>Illuminate\Foundation\Testing\WithConsoleEvents</code>特征添加到该类来为给定测试类启用这些事件：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token php language-php"><span class="token delimiter important">&lt;?php</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Foundation<span class="token punctuation">\</span>Testing<span class="token punctuation">\</span>WithConsoleEvents</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token function">uses</span><span class="token punctuation">(</span><span class="token class-name static-context">WithConsoleEvents</span><span class="token operator">::</span><span class="token keyword">class</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// ...</span></span>
<span class="line"></span></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token php language-php"><span class="token delimiter important">&lt;?php</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">namespace</span> <span class="token package">Tests<span class="token punctuation">\</span>Feature</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Foundation<span class="token punctuation">\</span>Testing<span class="token punctuation">\</span>WithConsoleEvents</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Tests<span class="token punctuation">\</span>TestCase</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">class</span> <span class="token class-name-definition class-name">ConsoleEventTest</span> <span class="token keyword">extends</span> <span class="token class-name">TestCase</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">use</span> <span class="token package">WithConsoleEvents</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// ...</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><blockquote>
<p>本译文仅用于学习和交流目的，转载请务必注明文章译者、出处、和本文链接<br>
我们的翻译工作遵照 <a href="https://learnku.com/docs/guide/cc4.0/6589" target="_blank" rel="noopener noreferrer">CC 协议</a>，如果我们的工作有侵犯到您的权益，请及时联系我们。</p>
</blockquote>
<hr>
<blockquote>
<p>原文地址：<a href="https://learnku.com/docs/laravel/11.x/console-testsmd/16711" target="_blank" rel="noopener noreferrer">https://learnku.com/docs/laravel/11.x/co...</a></p>
<p>译文地址：<a href="https://learnku.com/docs/laravel/11.x/console-testsmd/16711" target="_blank" rel="noopener noreferrer">https://learnku.com/docs/laravel/11.x/co...</a></p>
</blockquote>
</div></template>


